﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IRefundService : IAppService
    {
        List<RefundInfo> GetList(string OrderID);
        void Add(AddRefundInput input);
        void Update(UpdateRefundInput input);
        void Delete(string id );

        Refund GetModel(string Id);

        Refund GetModelByItemId(string ItemId);
        Refund GetModelByOrderId(string OrderID);

        void UpdateReFundStatu( string out_refund_no, string out_refund_status);


        PagedData<RefundInfo> GetPageData(Pagination page, int ST, int SortID, string keyword);
    }

    public class RefundService : AppServiceBase<Refund>, IRefundService
    {
        public RefundService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<RefundInfo> GetList(string OrderID)
        {
            string sql = "SELECT a.*,b.OrderCode,c.UserName,c.Mobile FROM refund a LEFT JOIN shoporder b ON a.OrderID=b.Id LEFT JOIN users c ON a.UserID=c.Id WHERE OrderID=?OrderID";
            List<RefundInfo> list = this.DbContext.SqlQuery<RefundInfo>(sql, new { OrderID = OrderID }).ToList();
            return list;
        }
         
        public void Add(AddRefundInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateRefundInput input)
        {
            this.UpdateFromDto(input);
        }

        public Refund GetModel(string Id)
        {
            return  this.Query.Where(a => a.Id == Id).FirstOrDefault();
        }

        public Refund GetModelByItemId(string ItemId)
        {
            return this.Query.Where(a => a.OrderItemID == ItemId).FirstOrDefault();
        }


        public Refund GetModelByOrderId(string OrderID)
        {
            return this.Query.Where(a => a.OrderID == OrderID).FirstOrDefault();
        }


        public void Delete(string id)
        {
            this.DbContext.Delete<Refund>(a => a.Id == id);
        }


        public void UpdateReFundStatu( string out_refund_no, string out_refund_status)
        {
            if (!string.IsNullOrEmpty(out_refund_no)&&!string.IsNullOrEmpty(out_refund_status))
            {
                this.DbContext.Update<Refund>(a => a.out_refund_no == out_refund_no, a => new Refund()
                {
                    out_refund_status = out_refund_status
                });
            }

            
        }


        public PagedData<RefundInfo> GetPageData(Pagination page,int ST,  int  SortID, string keyword)
        { 

            string strFileds = " a.*,b.OrderCode,c.UserName,c.Mobile ";

            string strWhere = "1=1 ";


            if (ST>-1)
            {
                strWhere += " and a.ST=" + ST;
            }
            if(SortID>-1)
            {
                strWhere += " and a.SortID =" + SortID;
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere += " and ( b.OrderCode like '%" + keyword + "%' or c.UserName like '%" + keyword + "%' or c.Mobile like '%" + keyword + "%')    ";
            }

            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", strFileds),
                new DbParam("?_tables", " refund a LEFT JOIN shoporder b ON a.OrderID=b.Id LEFT JOIN users c ON a.UserID=c.Id  "),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", "CreateDate desc"),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
        };

            List<RefundInfo> list = this.DbContext.SqlQuery<RefundInfo>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();

            PagedData<RefundInfo> pagedData = new PagedData<RefundInfo>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);




            return pagedData;
        }

          

    }

}
